package com.yoomiito.app.adapter.home.reommend

import android.content.Context
import android.graphics.drawable.GradientDrawable
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.isNull
import com.yoomiito.app.model.ViewType
import com.yoomiito.app.model.home.VideoClipInfo
import com.yoomiito.app.ui.video.clip.VideoClipListActivity
import com.yoomiito.app.utils.ColorUtils
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.widget.RoundImageView

class HomeItemVideoClipAdapter(val context: Context, val datas: List<ViewType<VideoClipInfo>>?,val data:ArrayList<VideoClipInfo>) : androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return if (22 == p1) {
            val view = LayoutInflater.from(context).inflate(R.layout.item_more_1, p0, false)
            ItemViewHolder(view)
        } else {
            val view = LayoutInflater.from(context).inflate(R.layout.item_home_video, p0, false)
            ItemGoodsViewHolder(view)
        }
    }

    override fun getItemCount(): Int = if (datas?.isNull()!!) 0 else datas.size

    override fun onBindViewHolder(p0: androidx.recyclerview.widget.RecyclerView.ViewHolder, p1: Int) {
        val videoClipInfo = datas?.get(p1)?.t
        if (datas?.get(p1)?.viewType == 22) {
            val holder = p0 as ItemViewHolder
            holder.mLookMoreTv.setOnClickListener {
                VideoClipListActivity.start(context, 10,data)
            }
            val lp = holder.itemView.layoutParams as androidx.recyclerview.widget.RecyclerView.LayoutParams
            lp.leftMargin = DensityUtil.dp2px(12f)
            lp.rightMargin = DensityUtil.dp2px(12f)
        } else {
            val holder = p0 as ItemGoodsViewHolder
            holder.videoBg.setOnClickListener {
                VideoClipListActivity.start(context, p1,data)
            }
            ImageLoader.getInstance().into(context, videoClipInfo?.video_img, holder.videoBg,R.drawable.default_clipvideo)
            holder.videoTitleTv.text = videoClipInfo?.title
        }
    }

    override fun getItemViewType(position: Int): Int = datas?.get(position)!!.viewType

    private class ItemGoodsViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
        val videoBg = itemView.findViewById<ImageView>(R.id.videoBg)
        val playVideo = itemView.findViewById<ImageView>(R.id.playVideo)
        val videoTitleTv = itemView.findViewById<TextView>(R.id.videoTitleTv)
    }

    private class ItemViewHolder(itemView: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView) {
        val mLookMoreTv: TextView = itemView.findViewById(R.id.item_look_more)
        val ll = itemView.findViewById<LinearLayout>(R.id.itemMoreLl)

        init {
            val lp = ll.layoutParams
            lp.width = DensityUtil.dp2px(130f)
            lp.height = ViewGroup.LayoutParams.MATCH_PARENT
            val drawable1 = ll.background as GradientDrawable
            drawable1.setColor(ResUtil.getColor(R.color.color_F4F4F4))
            drawable1.cornerRadius = DensityUtil.dp2px(8f).toFloat()

            val drawable = mLookMoreTv.background as GradientDrawable
            drawable.setColor(ResUtil.getColor(R.color.color_FF246C))

        }
    }

}